package tech.darkblue.rpcui.extend;

import java.io.OutputStream;
import java.io.PrintStream;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

public class LoggerPrintStream extends PrintStream {

    private Text loggerText;

    public LoggerPrintStream(OutputStream out, Text logger) {
        super(out);
        loggerText = logger;
    }

    @Override
    public void write(byte[] buf, int off, int len) {
        String message = new String(buf, off, len);
        Display.getDefault().syncExec(new Runnable() {
            @Override
            public void run() {
                if (loggerText != null && !loggerText.isDisposed()) {
                    loggerText.append(message);
                }
            }
        });
    }

}
